package com.example.langchainrag.controller;




import com.example.langchainrag.service.Assistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.splitter.DocumentByLineSplitter;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/api/rag")
@RequiredArgsConstructor
public class RagController {
    final ChatLanguageModel chatLanguageModel;

    final EmbeddingStore<TextSegment> embeddingStore;

//    final EmbeddingModel embeddingModel;


    /**
     * 加载文件到向量数据库
     * localhost:8081/api/rag/load
     *
     * @return
     */
    @GetMapping("/load")
    public Object load() {
        List<Document> documents = FileSystemDocumentLoader.loadDocuments("E:\\demo\\LangchainRag\\src\\main\\resources\\file");
        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
        ;
        System.out.println("向量化成功");
        return "success";
    }

//    @GetMapping("/load2")
//    public Object load2() {
//        List<Document> documents = FileSystemDocumentLoader.loadDocuments("E:\\demo\\LangchainRag\\src\\main\\resources\\file");
//        EmbeddingStoreIngestor.builder().embeddingStore(embeddingStore)
//                .embeddingModel(embeddingModel)
//                .documentSplitter(new DocumentByLineSplitter(30,20))
//                .build().ingest(documents);
//        ;
//        return "success";
//    }

    final Assistant assistant;

    /**
     * 聊天对话
     * localhost:8081/api/rag/high/chat?message=AI去中心化与AI伦理成为焦点是什么
     *
     * @return
     */
    @GetMapping("/high/chat")
    public Object highChat(@RequestParam(value = "message") String message) {
        return assistant.chat(message);
    }
}
